package com.suray.wcs.web.system.controller;

import com.suray.commin.annotation.Log;
import com.suray.commin.core.controller.BaseController;
import com.suray.commin.core.page.TableDataInfo;
import com.suray.commin.core.utils.ExcelUtil;
import com.suray.commin.enmus.BusinessType;
import com.suray.commin.core.controller.ResponseData;
import com.suray.wcs.service.core.util.ReturnTypeBox;
import com.suray.wcs.service.enums.WcsExceptionType;
import com.suray.wcs.service.system.entity.ExceptionLogDB;
import com.suray.wcs.service.system.entity.RgvHeartLogDB;
import com.suray.wcs.service.system.service.impl.ExceptionLogServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:异常日志
 * @author: rongbj
 * @date:2019年10月19日 下午5:20:36
 */
@RestController
@RequestMapping("/exceptionLog")
public class ExceptionLogController extends BaseController {
    @Autowired
    private ExceptionLogServiceImpl exceptionLogService;

    /**
     * 分页查询
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('exceptionLog:list')")
    @ApiOperation(value = "查询系统日志")
    @GetMapping(value = "/list")
    @ResponseBody
    public TableDataInfo getWCSException(ExceptionLogDB exceptionLogdb) {
        startPage();
        List<ExceptionLogDB> result = exceptionLogService.getExceptionLogs(exceptionLogdb);
        return getDataTable(result);
    }

    @PreAuthorize("@ss.hasPermi('exceptionLog:export')")
    @ApiOperation(value = "导出异常日志")
    @Log(title = "异常日志", method = "导出异常日志", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ResponseData export(ExceptionLogDB exceptionLogDB) {
        List<ExceptionLogDB> list = exceptionLogService.getExceptionLogs(exceptionLogDB);
        ExcelUtil<ExceptionLogDB> util = new ExcelUtil<>(ExceptionLogDB.class);
        return util.exportExcel(list, "异常日志");
    }

    @PostMapping("/getExceptionType")
    @ApiOperation("获取系统异常类型")
    @ResponseBody
    public ResponseData getExceptionType() {
        return ResponseData.success(ReturnTypeBox.returnTypeBox(WcsExceptionType.class));
    }

    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @Log(title = "异常日志", method = "删除操作日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteExceptionLog/{exceptionLogIds}")
    public ResponseData remove(@PathVariable Long[] exceptionLogIds) {
        return toAjax(exceptionLogService.deleteExceptionLogById(exceptionLogIds));
    }

    @Log(title = "异常日志", method = "清空操作日志", businessType = BusinessType.CLEAN)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @DeleteMapping("/cleanExceptionLog")
    public ResponseData clean() {
        exceptionLogService.cleanExceptionLog();
        return ResponseData.success();
    }

    /**
     * 查询图表数据
     *
     * @param exceptionLogDB
     * @return
     */
    @PreAuthorize("@ss.hasPermi('exceptionLog:query')")
    @ApiOperation(value = "查询图表数据")
    @PostMapping(value = "/getExceptionDate")
    @ResponseBody
    public ResponseData getExceptionDate(ExceptionLogDB exceptionLogDB) {
        try {
            Date sendTime = exceptionLogDB.getCreateTime();
            Date sendTimeStart;
            Date sendTimeEnd;
            if (sendTime == null) {
                sendTimeEnd = new Date();
            } else {
                sendTimeEnd = sendTime;
            }
            sendTimeStart = DateUtils.addDays(sendTimeEnd, -15);
            Map<String, Object> params = new HashMap<>();
            params.put("beginTime", sendTimeStart);
            params.put("endTime", sendTimeEnd);
            exceptionLogDB.setParams(params);
            List<Map<String, Object>> datalist = exceptionLogService.getExceptionDate(exceptionLogDB);
            return ResponseData.success("查询成功", datalist);
        } catch (Exception e) {
            return ResponseData.error(e.getMessage());
        }
    }
}
